/**
 * 标准弹窗工具
 */

export interface DialogInfo {
	/**
	 * 提示的标题
	 */
	title ?: string;
	/**
	 * 提示的内容
	 */
	content ?: string;
	/**
	 * 是否显示取消按钮，默认为 true
	 */
	showCancel ?: boolean;
	/**
	 * 取消文本
	 */
	cancelText ?: string;
	/**
	 * 确认文本
	 */
	confirmText ?: string;
	/**
	 * 点击确认
	 */
	confirmPressed ?: DialogCallback;
	/**
	 * 点击取消
	 */
	cancelPressed ?: DialogCallback;
}

type DialogCallback = () => void

export const showCommonDialog = (dialog : DialogInfo) => {
	uni.showModal({
		title: dialog.title,
		content: dialog.content,
		showCancel: dialog.showCancel ?? true,
		cancelText: dialog.cancelText ?? "取消",
		confirmText: dialog.confirmText ?? "确定",
		cancelColor: "#999999",
		confirmColor: "#E8363D",
		success: (result) => {
			if (result.confirm && dialog.confirmPressed) {
				dialog.confirmPressed()
				return
			}
			if (result.cancel && dialog.cancelPressed) {
				dialog.cancelPressed()
				return
			}
		}
	})
}

/**
 * 底部弹窗ActionSheet
 */
type ActionSheetCallback = (tapIndex : number) => void

export interface ActionSheetData {
	/**
	 * 提示的标题
	 */
	title ?: string;
	/**
	 * 选择的内容
	 */
	itemList : any[];
	/**
	 * 按钮的文字颜色，默认为"#000000"
	 */
	itemColor ?: string.ColorString | string;
	/**
	 * 成功回调
	 */
	callback : ActionSheetCallback
}

export const actionSheet = (data : ActionSheetData) => {
	uni.showActionSheet({
		title: data.title ?? '请选择',
		itemList: data.itemList,
		itemColor: data.itemColor ?? '#333333',
		success: (res) => {
			log('选中的内容:' + res.tapIndex)
			data.callback(res.tapIndex)
		},
		fail: (err: any) => {
			//处理错误情况
			log('操作失败', JSON.stringify(err))
		}
	})
}